/*********************************Field组件*********************************************/
(function($) {
/*********************************Field公共方法*********************************************/
jQuery.fn.fieldValue = function(value){
	var instance = getInstance(this);
	if(instance){
		if(!value){
			return instance.getValue();
		}else{
			instance.setValue(value);
		}
	}
	return null;
};

jQuery.fn.fieldReset = function(){
	var instance = getInstance(this);
	if(instance){
		return instance.reset();
	}
};
/***************************************私有方法**********************************************/
function getInstance(element){
	if(element.is('.youi-field')){
		var instance = $.data(element[0], element.attr('fieldType'));
		return instance;
	}
	return null;
}
/***************************************field 接口**********************************************/
$.youi.field = {
	init:function(){
		this.options = $.extend({},$.youi.field.defaults,$.youi[this.widgetName].defaults, this.options);
		
		this.element.addClass('youi-field');
		this.element.attr('ref-property',this.options.property);
		this.element.attr('fieldType',this.widgetName);
		this.addCaption();
		this.fieldBody = $('<div class="youi-field-body"/>');
		this.element.append(this.fieldBody);
		
		if(this.options.caption){
			this.fieldBody.css({position:'absolute',left:this.options.labelWidth|80,top:1});
		}else{
			if(!$.browser.mozilla){
				this.fieldBody.css({position:'absolute',left:0,top:0});
			}
		}
		
		if(this.initField()==false){
			$.youi.log.info('必须实现的初始化方法');
			this.element.empty();
		}
	},
	
	addCaption:function(){
		if(this.options.caption){
			this.element.append('<label for="'+this.options.property+'" class="youi-field-caption">'+this.options.caption+'\uff1a</label>');
		}
	},
	/*********************子类必须实现的方法*************************************/
	initField:function(){
		return false;
	},

	getValue:function(){
		return this.element.find('[name='+this.options.property+']').val();
	},
	
	setValue:function(value){
		this.element.find('[name='+this.options.property+']').val(value);
	},
	
	reset:function(){
		this.element.find('[name='+this.options.property+']').val('');
	}
};

$.youi.field.defaults={
	dataType:'string'
};

/***************************************文本输入框组件**********************************************/
$.widget("youi.fieldText", $.extend($.youi.field,{
	initField:function(){
		var fieldTextInput = $('<input id="'+this.options.property+'" name="'+this.options.property+'" class=\"youi-field-text\" type="text"/>');
		this.fieldBody.append(fieldTextInput);
	}
}));

$.youi.fieldText.deafults = {};
/***************************************下拉选择框组件**********************************************/
$.widget("youi.fieldSelect", $.extend($.youi.field,{
	initField:function(){
		var self = this;
		this.items = [];
		this.selectBody = $('<div class="youi-field-select-body"/>');
		var selectInput = $('<input readonly="true" class="youi-field-selectText" type="text"/>');
		if(!$.browser.msie){
			selectInput.css('paddingTop','2px');
		}
		selectInput.width(this.options.width);
		
		this.selectBody.append(selectInput).append('<input name="'+this.options.property+'"  type=\"hidden\"/>');
		
		this.selectBody.mousedown(function(){
			self.trigger();
			return false;
		});
		
//		if($.browser.mozilla){
//			this.selectBody.get(0).addEventListener('DOMMouseScroll', function(){return false;}, false);
//		}
		
		this.fieldBody.append(this.selectBody);
		this.createOptionsPanel();
	},
	
	createOptionsPanel:function(){
		this.optionsPanelId = 'youi-combo-list-'+this.options.property.replace('.','-');
		this.optionsPanel = $('<div id="'+this.optionsPanelId+'" class="youi-combo-list" style="position: absolute; z-index: 1000000; left: -10000px; top: -10000px;"/>');
		this.optionsPanel.bind('mousedown',function(){return false;}).bind('mousewheel',function(event){event.stopPropagation();});
		
		$('body',document).append(this.optionsPanel);
	},
	
	loadData:function(afterLoad){
		var self = this;
		var params = '';
		if(this.options.src){
			$.ajax({
				type: this.options.ajaxType,
				url:  this.options.src,
				dataType:this.options.srcType,
				data: params,
				success: function(result){
					var datas = $.youi.parseDatas(result,self.options.srcType);
					self.addItem('','');
					self.addItems(datas.records);
					self.hasLoad = true;
					if($.isFunction(afterLoad)){
						afterLoad();
					}
				}
			});
		}
	},
	
	trigger:function(){
		if($.youi.fieldSelect.expanded){
			this.collapse();
		}else{
			this.expand();
		}
	},
	
	expand:function(){
		var self = this;
		if(!this.hasLoad){
			this.loadData(function(){self.setOptionsPanelScroll();});
		}
		//alert();
		this.optionsPanel.show().css({
			left:this.selectBody.offset().left,
			top:this.selectBody.offset().top+this.selectBody.innerHeight()+1,
			width:this.fieldBody.innerWidth()-2
		});
		$.youi.fieldSelect.openOptionsPanelId=this.optionsPanelId;
		$('body',document).bind('mousedown',$.youi.fieldSelect.collapse).bind('mousewheel',$.youi.fieldSelect.collapse);
		
		if($.browser.mozilla){
			window.addEventListener('DOMMouseScroll', $.youi.fieldSelect.collapse, false);
		}
		$.youi.fieldSelect.expanded = true;
	},
	
	collapse:function(){
		$.youi.fieldSelect.collapse();
	},
	
	setOptionsPanelScroll:function(){
		var optionsPanel = this.optionsPanel;
		
		if(optionsPanel.outerHeight()>250){
			optionsPanel.css({
				height		:250,
				overflowY	:'auto'
			});
		}
		optionsPanel = null;
	},
	
	addItems:function(records){
		var self = this;
		$.each(records,function(i){
			self.addItem(this[self.options.code],this[self.options.show]);
		});
	},
	
	addItem:function(code,show){
		if(this.items[code]==null){
			var self = this;
			var item = $('<div class="youi-combo-list-item" code="'+code+'" title="'+show+'">'+show+'</div>');
			item.bind('mouseover',function(){
				$(this).addClass('youi-combo-list-item-over');
			}).bind('mouseout',function(){
				$(this).removeClass('youi-combo-list-item-over');
			}).bind('mousedown',function(){
				return false;
			}).bind('click',function(){
				self.setValue(this.getAttribute('code'));
				self.collapse();
			});
			
			this.optionsPanel.append(item);
			this.items[code] = show;
			item = null;
		}
	},
	
	setValue:function(value){
		var item = this.optionsPanel.find('.youi-combo-list-item[@code='+value+']');
		this.selectBody.find(':text').val(item.text());
		this.selectBody.find(':hidden').val(value);
	},
	
	getValue:function(){
		return this.selectBody.find(':hidden').val();
	},
	
	reset:function(){
		this.selectBody.find(':text').val('');
		this.selectBody.find(':hidden').val('');
	}
}));

$.extend($.youi.fieldSelect,{
	defaults:{
		ajaxType:"GET",
		srcType:"json"
	},
	
	collapse:function(){
		$('#'+$.youi.fieldSelect.openOptionsPanelId).hide();
		$.youi.fieldSelect.expanded = false;
		$.youi.fieldSelect.openOptionsPanelId = null;
		$('body',document).unbind('mousedown',$.youi.fieldSelect.collapse).unbind('mousewheel',$.youi.fieldSelect.collapse);
	
		if($.browser.mozilla){
			window.removeEventListener('DOMMouseScroll',$.youi.fieldSelect.collapse);
		}
	}
});
/***********************************************/
$.widget("youi.fieldRadios", $.extend($.youi.field,{
		initField:function(){
			if(this.options.items){
				this.addItems(this.options.items);
			}
		},
		
		addItems:function(items){
			for(var i=0;i<items.length;i++){
				this.addItem(items[i][this.options.code],items[i][this.options.show]);
			}
		},
		
		addItem:function(code,show){
			this.fieldBody.append('<input type="radio" value="'+code+'" name="'+this.options.property+'">'+show+' </input>');
		},
		
		setValue:function(value){
			this.fieldBody.find('input:radio[@value='+value+']').attr("checked",true) ;
		},
		
		getValue:function(){
			return this.fieldBody.find('input:radio[@checked]').val();
		},
		
		reset:function(){
			this.fieldBody.find('input:radio[@checked]').removeAttr('checked');
		}
	}
));

$.extend($.youi.fieldRadios,{
		defaults:{
			code:'code',
			show:'show'
		}
	}
);

})(jQuery);